package com.yx.doodleview.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.shareboard.ShareBoardConfig;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.yx.baselibrary.eventbus.EventCenter;
import com.yx.baselibrary.image.ImageSelectActivity;
import com.yx.baselibrary.utils.IntentUtil;
import com.yx.doodleview.R;
import com.yx.doodleview.bean.ColorBean;
import com.yx.doodleview.bean.ShareInfo;
import com.yx.doodleview.dialog.ChooseDialog;
import com.yx.doodleview.ui.base.BaseActivity;
import com.yx.doodleview.ui.widget.doodleveiw.DoodleView;
import com.yx.doodleview.util.BitmapUtil;
import com.yx.doodleview.util.FileUtils;
import com.yx.doodleview.util.ShareUtils;
import com.yx.filemanager.FileManagerActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements ChooseDialog.OnItemViewClickListener {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.doodle_view)
    DoodleView doodleView;
    private List<ColorBean> list = new ArrayList<>();//画笔颜色数据
    private List<ColorBean> list2 = new ArrayList<>();//画笔粗细数据
    private ChooseDialog chooseDialog;
    private int[] color = new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.BLACK};
    private String[] texts = new String[]{"红", "绿", "蓝", "黑"};
    private String[] widths = new String[]{"细", "中", "粗"};
    private ChooseDialog chooseWithDialog;
    private ShareAction mShareAction;
    private String path;


    @Override
    public void before(Bundle savedInstanceState) {

    }

    @Override
    public void initViewAndEvent() {
        //用Toolbar创建menu
        mToolbar.inflateMenu(R.menu.menu);
        //设置popupstyle.比如是否覆盖描点,背景,字体颜色什么的.必须在inflateMenu()之前设置
        mToolbar.setPopupTheme(R.style.popup_theme);

        //拿到Menu
        Menu menu = mToolbar.getMenu();
        //下面的这段代码是为了让menu菜单折叠样式时,展开能显示icon图标.不然icon图标不会显示.(感觉很坑)
        if (menu != null) {
            if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
                try {
                    MenuBuilder menuBuilder = (MenuBuilder) menu;
                    menuBuilder.setOptionalIconsVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        setSupportActionBar(mToolbar);


        for (int i = 0; i < color.length; i++) {
            ColorBean colorBean = new ColorBean();
            colorBean.setColor(color[i]);
            colorBean.setText(texts[i]);
            list.add(colorBean);
        }

        for (int i = 0; i < widths.length; i++) {
            ColorBean colorBean = new ColorBean();
            colorBean.setText(widths[i]);
            list2.add(colorBean);
        }

         /*增加自定义按钮的分享面板*/
        mShareAction = new ShareAction(MainActivity.this).setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE,
                SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
                        ShareInfo shareInfo = new ShareInfo();
                        shareInfo.setImageUrl(path);
                        ShareUtils.share(MainActivity.this, shareInfo, new ShareUtils.CallBackListener() {
                            @Override
                            public void onCallBack() {

                            }
                        }, share_media);
                    }
                });
    }


    @Override
    public int getContentViewLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public View getLoadingTargetView() {
        return null;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_color:
                if (chooseDialog == null) {
                    chooseDialog = ChooseDialog.newInstance(list);
                    chooseDialog.setOnItemClickListener(this);
                }
                chooseDialog.show(getSupportFragmentManager(), "");
                break;
            case R.id.item_width:
                if (chooseWithDialog == null) {
                    chooseWithDialog = ChooseDialog.newInstance(list2);
                    chooseWithDialog.setOnItemClickListener(new ChooseDialog.OnItemViewClickListener() {
                        @Override
                        public void onItemClick(ColorBean colorBean) {
                            switch (colorBean.getText()) {
                                case "细":
                                    doodleView.setStokenWidth(1);
                                    break;
                                case "中":
                                    doodleView.setStokenWidth(3);
                                    break;
                                case "粗":
                                    doodleView.setStokenWidth(5);
                                    break;
                            }

                        }
                    });
                }
                chooseWithDialog.show(getSupportFragmentManager(), "");
                break;
            case R.id.item_clear:
                doodleView.reset();
                break;
            case R.id.item_save:
                requestReadAndWriteSDPermission(new PermissionHandler() {

                    @Override
                    public void onGranted() {
                        Bitmap bitmap = doodleView.getBitmap();
                        FileUtils.savePhotoToSDCard(bitmap, FileUtils.getDir("") + "yx" + ".png");
                        path = FileUtils.getDir("") + "yx" + ".png";
                        ShareBoardConfig config = new ShareBoardConfig();
                        config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_NONE);
                        mShareAction.open(config);


                    }

                    @Override
                    public void onDenied() {
                        IntentUtil.showToast("请打开权限，以便功能正常使用");
                    }
                });

                break;
            case R.id.item_file:
                requestReadAndWriteSDPermission(new PermissionHandler() {
                    @Override
                    public void onGranted() {
                        IntentUtil.readyGo(MainActivity.this, FileManagerActivity.class);
                    }
                });

                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(ColorBean colorBean) {
        doodleView.setColor(colorBean.getColor());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }


    @OnClick(R.id.tv_alum)
    public void onClick() {
        ImageSelectActivity.startImageSelectActivity(this, true);
    }


    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    public void onEventComming(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == ImageSelectActivity.EVENT_CHOOSE_PHOTO) {
            ArrayList<String> strings = (ArrayList<String>) eventCenter.getData();
            Bitmap bitmap = BitmapUtil.compressionFiller(strings.get(0));
            doodleView.setBackgroundBitmap(bitmap);
        }
        super.onEventComming(eventCenter);
    }
}
